import mx.controls.Alert;
import mx.events.ListEvent;

import net.optix.ip.events.GetTradeMarkActionsEvent;
import com.adobe.cairngorm.control.CairngormEventDispatcher;
import net.optix.ip.model.ModelLocator;
import net.optix.ip.vo.ActionVO;
[Bindable]
private var model:ModelLocator	=	ModelLocator.getInstance();

[Bindable]
[Embed("/assets/folder_close.png")]
private var actionIcon:Class;

[Bindable]
[Embed("/assets/hoja.png")]
private var fileIcon:Class;

private function initApp():void
{
	var getTradeMarkActions:GetTradeMarkActionsEvent	=	new GetTradeMarkActionsEvent(model.currentTradeMark.tradeMarkID);
	CairngormEventDispatcher.getInstance().dispatchEvent(getTradeMarkActions);
}
private function showTreeIcon(item:Object):*
{
	var iconClass:Class;
	switch(XML(item).localName())
	{
		case "actions":
			iconClass	=	actionIcon;
			break;
		case "action":
			iconClass	=	actionIcon;
			break;
		case "File":
			iconClass	=	fileIcon;
			break;
	}
	return iconClass;
}
[Bindable]
private var bAction:int		=	-1;
[Bindable]
private var bVisible:Boolean	=	false;
private var downloadURL:URLRequest;
private var file:FileReference;
private var fileName:String;
public function download():void 
{
	bVisible	=	true;
	if(this.proBar != null)
	{
		proBar.label	=	"";
	}

   	downloadURL = new URLRequest(model.urlH+"/Drawing/"+fileName);
   
    file = new FileReference();
    configureListeners(file);
   	file.download(downloadURL);
}

private function configureListeners(dispatcher:IEventDispatcher):void 
{
   	dispatcher.addEventListener(Event.CANCEL, cancelHandler);
    dispatcher.addEventListener(Event.COMPLETE, completeHandler);
	dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
	dispatcher.addEventListener(Event.OPEN, openHandler);
	dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
	dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
	dispatcher.addEventListener(Event.SELECT, selectHandler);
}

private function cancelHandler(event:Event):void 
{
	
}
	
private function completeHandler(event:Event):void 
{
	var title:String	=	"Result of download file ";
	var message:String	=	"Successfully download the description file";
	
	var alert:Alert	=	 
		Alert.show(message,title,Alert.OK, null,null, null,  Alert.OK );
	alert.width		=	360;
	alert.height	=	130;
	bVisible	=	false;
}
	
private function ioErrorHandler(event:IOErrorEvent):void 
{
	var title:String	=	"Result of download file ";
	var message:String	=	"There is some error in downloading the file";
	
	var alert:Alert	=	 
	Alert.show(message,title,Alert.OK, null,null, null,  Alert.OK );
	alert.width		=	360;
	alert.height	=	130;
	bVisible	=	false;
}
	
private function openHandler(event:Event):void 
{
	trace(event);        
}
	
private function progressHandler(event:ProgressEvent):void 
{
	trace(event.toString());
	var proc: uint = event.bytesLoaded / event.bytesTotal * 100;
	proBar.setProgress(proc, 100);
    proBar.label= proc + "%";
}


	
private function securityErrorHandler(event:SecurityErrorEvent):void 
{
	           
}
	
private function selectHandler(event:Event):void 
{
	var file:FileReference = FileReference(event.target);
}

[Bindable]
private var action:ActionVO		=	new ActionVO();

private function tree_itemClick(evt:ListEvent):void 
{	
	if(actions.selectedIndex >= 0)
  	{	
	  	var node:XML	=		XML(actions.selectedItem);	
		if(node.localName() == "action")
		{
			this.bAction	=	1;
			this.action.actionID	=	int(node.@ActionID);
			this.action.userPost	=	node.@UserPost;
			this.action.type		=	int(node.@Type);
			this.action.date		=	node.@Date;
			this.action.description	=	node.@Description;
			this.action.dueDate		=	node.@DueDate;
			this.action.cost		=	node.@Cost;
		}
		else if(node.localName() == "File" && node.@label != "")
		{
			this.bAction	=	2;
			this.fileName	=	node.@label;
		}
		else
		{
			this.bAction	=	0;
		}
  	}
  	else
  	{
  		this.bAction	=	0;
  	}
} 